package com.dl698.axdr;

import com.dl698.asn1.axdr.AxdrType;
import com.dl698.util.ReverseByteArrayInputStream;
import com.dl698.util.ReverseByteArrayOutputStream;

/**
 * Time类
 * 实现时间的编解码
 * 
 * @author wind
 * @version 1.1.1
 */
public class Time implements AxdrType {

    private byte[] dataCode = null;
    private Unsigned8 hour = null;
    private Unsigned8 minute = null;
    private Unsigned8 second = null;

    /**
     * 默认构造函数
     */
    public Time() {
    }

    /**
     * 设置数据编码
     * 
     * @param dataCode 数据编码
     */
    public void setDataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    /**
     * 设置时间参数
     * 
     * @param hour   小时
     * @param minute 分钟
     * @param second 秒
     */
    public void setTime(Unsigned8 hour, Unsigned8 minute, Unsigned8 second) {
        this.hour = hour;
        this.minute = minute;
        this.second = second;
    }

    /**
     * 设置小时
     * 
     * @param hour 小时
     */
    public void setHour(Unsigned8 hour) {
        this.hour = hour;
    }

    /**
     * 设置分钟
     * 
     * @param minute 分钟
     */
    public void setMinute(Unsigned8 minute) {
        this.minute = minute;
    }

    /**
     * 设置秒
     * 
     * @param second 秒
     */
    public void setSecond(Unsigned8 second) {
        this.second = second;
    }

    @Override
    public int decode(ReverseByteArrayInputStream input) {
        int codeLength = 0;

        hour = new Unsigned8();
        hour.setConst();
        codeLength += hour.decode(input);

        minute = new Unsigned8();
        minute.setConst();
        codeLength += minute.decode(input);

        second = new Unsigned8();
        second.setConst();
        codeLength += second.decode(input);

        return codeLength;
    }

    @Override
    public int encode(ReverseByteArrayOutputStream output) {
        int codeLength;
        if (dataCode != null) {
            codeLength = dataCode.length;
            for (int i = dataCode.length - 1; i >= 0; i--) {
                try {
                    output.write(dataCode[i]);
                } catch (java.io.IOException e) {
                    throw new RuntimeException("IO exception during encode", e);
                }
            }
        } else {
            if (hour != null && minute != null && second != null) {
                codeLength = 0;
                codeLength += second.encode(output);
                codeLength += minute.encode(output);
                codeLength += hour.encode(output);
            } else {
                throw new RuntimeException("time is null");
            }
        }
        return codeLength;
    }

    /**
     * 编码并保存
     * 
     * @param encodingSizeGuess 编码大小预估
     */
    public void encodeAndSave(int encodingSizeGuess) {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        encode(revOStream);
        this.dataCode = revOStream.getArray();
    }

    @Override
    public String toString() {
        if (hour != null && minute != null && second != null) {
            return "sequence: {" + "hour: " + hour + ", minute: " + minute + ", second: " + second + "}";
        } else {
            return "time is null";
        }
    }

    /**
     * 获取数据编码
     * 
     * @return 数据编码
     */
    public byte[] getDataCode() {
        return dataCode;
    }
}